﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Business;
using AMM.Model;
using AMM.Model.Business;

namespace AMM.UserControls {
    public partial class ProduceConductSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int customerOrderValue = Convert.ToInt32(this.ddlCustomerOrder.SelectedValueEx);
            int productConductTypeValue = Convert.ToInt32(this.cmbProduceConductType.SelectedValue);

            ProduceConductSearchModel searchResult = new ProduceConductSearchModel();
            searchResult.OrderDateStart = this.GetDateTimePickerValue(this.dtpOrderStart);
            searchResult.OrderDateEnd = this.GetDateTimePickerValue(this.dtpOrderEnd);
            searchResult.CustomerOrderId = customerOrderValue;
            searchResult.ProduceConductTypeId = productConductTypeValue;

            if (customerOrderValue == 0) { searchResult.CustomerOrderId = null; }
            if (productConductTypeValue == 0) { searchResult.ProduceConductTypeId = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.dtpOrderStart.Checked = false;
            this.dtpOrderEnd.Checked = false;
            this.ddlCustomerOrder.SelectedValueEx = null;
            this.cmbProduceConductType.SelectedValue = 0;
        }

        public ProduceConductSearchControl() {
            this.InitializeComponent();
        }

        private void BindCustomerOrder() {
            CustomerOrderSearchModel searchModel = new CustomerOrderSearchModel();
            CustomerOrderProcessor customerProcessor = new CustomerOrderProcessor(int.MaxValue);
            List<CustomerOrderViewModel> customerList = customerProcessor.FetchCustomerOrderViewModelList(searchModel).ToList();

            this.ddlCustomerOrder.DisplayMemberEx = "OrderNo";
            this.ddlCustomerOrder.ValueMemberEx = "Key";
            this.ddlCustomerOrder.HeightEx = 300;
            this.ddlCustomerOrder.WidthEx = 400;
            this.ddlCustomerOrder.PageSizeEx = 20;
            this.ddlCustomerOrder.DataBind(new CustomerOrderViewModel(), x => new { x.OrderNo, x.CustomerCode, x.CustomerName }, customerList);
        }

        private void ProduceConductSearchControl_Load(object sender, EventArgs e) {
            this.BindCustomerOrder();
            this.BindBasics(this.cmbProduceConductType, BasicsType.ProduceConductType);
        }
    }
}
